package com.aoxin.util;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.aoxin.model.course.SchoolCoursePage;
import com.aoxin.model.course.SlfClass;
import com.aoxin.model.course.SlfClassChildren;
import com.aoxin.model.dto.ChildrenCoursePackageDto;
import com.aoxin.service.inter.course.systemCourse.SchoolCoursePageService;
import com.aoxin.service.inter.course.webCourse.ClassChildrenService;
import com.aoxin.service.inter.course.webCourse.ClassService;
import com.aoxin.service.inter.course.webCourse.MyCoursePageService;

/**
 * 定时器
 * 
 * @author Melody
 * @date 2018年5月7日 下午3:12:48
 */

@Component
public class ScheduledConfig {

	private static Log log = LogFactory.getLog(ScheduledConfig.class);

	@Autowired
	private MyCoursePageService myCoursePageService;

	@Autowired
	private ClassService classService;

	@Autowired
	private ClassChildrenService classChildrenService;

	@Autowired
	private SchoolCoursePageService schoolCoursePageService;

	/**
	 * 创建班级 每天晚上11点执行
	 */
	@Scheduled(cron = "0 0 23 * * ?")
	// @Scheduled(cron = "*/20 * * * * ?")
	public void createClass() {
		// 针对B类课程，只要购买过B类课程的孩子。系统将自动排班。
		// 查询还未排班的课程包
		List<ChildrenCoursePackageDto> childrenCoursePackageDtos = this.myCoursePageService.getChildrenCoursePackage();
		if (childrenCoursePackageDtos.size() > 0) {
			for (ChildrenCoursePackageDto childrenCoursePackageDto : childrenCoursePackageDtos) {
				List<Integer> coursePageIds = this.myCoursePageService.getCoursePageByUnallotted(childrenCoursePackageDto.getChildrenId(), childrenCoursePackageDto.getCoursePackageId());
				saveClass(childrenCoursePackageDto, coursePageIds);
			}
		} else {
			List<Integer> coursePageIds = this.myCoursePageService.getCoursePageByUnallotted(null, null);
			saveClass(null, coursePageIds);
		}

	}

	private void saveClass(ChildrenCoursePackageDto childrenCoursePackageDto, List<Integer> coursePageIds) {
		Integer childrenId = null;
		Integer coursePackageId = null;
		Calendar calendar = Calendar.getInstance();
		Date date = new Date();
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH + 1);
		int day = calendar.get(Calendar.DAY_OF_MONTH);
		if (childrenCoursePackageDto != null) {
			childrenId = childrenCoursePackageDto.getChildrenId();
			coursePackageId = childrenCoursePackageDto.getCoursePackageId();
		}
		int count = 0;
		for (Integer coursePageId : coursePageIds) {
			while (true) {
				List<Integer> childrens = this.myCoursePageService.getChildrenByCoursePage(childrenId, coursePackageId, coursePageId);
				// TODO 判断人数是否达到可开班人数
				if (childrens.size() > 11) {
					try {
						log.info("***************达到开班条件！");
						// 查询学校课程包信息
						SchoolCoursePage schoolCoursePage = this.schoolCoursePageService.getSchoolCoursePageById(coursePageId);
						// 创建班级
						SlfClass slfClass = new SlfClass();
						count++;
						slfClass.setName(schoolCoursePage.getName() + "[" + String.valueOf(year) + String.valueOf(month) + String.valueOf(day) + "-" + count + "]" + "班");
						log.info("***************班级名称：" + schoolCoursePage.getName() + "[" + String.valueOf(year) + String.valueOf(month) + String.valueOf(day) + "]" + "班");
						slfClass.setCoursePackageId(String.valueOf(coursePageId));
						slfClass.setNikeName(schoolCoursePage.getName() + "[" + String.valueOf(year) + String.valueOf(month) + String.valueOf(day) + "]" + "班");
						slfClass.setCoursePackageId(String.valueOf(coursePageId));
						slfClass.setStartUpCourseDate(date);
						slfClass.setCreateDate(date);
						Integer classId = this.classService.insertClass(slfClass);

						for (Integer children : childrens) {
							SlfClassChildren slfClassChildren = new SlfClassChildren();
							slfClassChildren.setChildrenId(children);
							slfClassChildren.setClassId(classId);
							this.classChildrenService.insertClassChildren(slfClassChildren);
						}
					} catch (IllegalAccessException e) {
						e.printStackTrace();
					}
				} else {
					log.info("****************未达到开班条件！");
					break;
				}
			}
		}
	}
}
